//
//  ChatBasicCell.swift
//  ModernFoodWebsite
//
//  Created by apple on 16/6/1.
//  Copyright © 2016年 Lonnie. All rights reserved.
//

import UIKit

class ChatBasicCell: UITableViewCell {
    var timeLabel = UILabel()
    var timeHeightConstraint:NSLayoutConstraint?
    var showTime = false {
        didSet {
            //self.timeLabel.hidden = !showTime
          timeHeightConstraint?.constant = showTime ? 35 : 0
        }
    }
    
    var fromMe = false {
        didSet {
           adjustContent()
        }
    }
    
    
    var headPotrait:UIImageView = UIImageView()
    var animationImageView = UIImageView()
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
   
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        selectionStyle = .None
        contentView.backgroundColor = UIColor.clearColor()
        backgroundView = UIView()
        contentView.addSubviews([timeLabel,headPotrait])
        timeLabel.textColor = UIColor(red: 0x99/255.0, green: 0x99/255.0, blue: 0x99/255.0, alpha: 1.0)

        timeLabel.snp_remakeConstraints { (make) in
            make.centerX.top.equalTo(self.contentView)
            make.height.equalTo(35)
        }
        timeLabel.constraints.forEach { (constraint) in
            if constraint.firstAttribute == .Height {
                timeHeightConstraint = constraint
            }
        }
        headPotrait.layer.cornerRadius = 20
        headPotrait.layer.masksToBounds = true
        headPotrait.snp_makeConstraints { (make) in
            make.leading.equalTo(self.contentView).offset(21)
            make.top.equalTo(self.timeLabel.snp_bottom).offset(17.5)
            make.size.equalTo(CGSizeMake(40, 40))
            make.bottom.lessThanOrEqualTo(self.contentView).offset(-10)
        }
    }
    
    func adjustContent() {
        headPotrait.snp_remakeConstraints { (make) in
            if fromMe {
                make.trailing.equalTo(self.contentView).offset(-21)
            } else {
                make.leading.equalTo(self.contentView).offset(21)
            }
            make.top.equalTo(self.timeLabel.snp_bottom).offset(17.5)
            make.size.equalTo(CGSizeMake(40, 40))
            make.bottom.lessThanOrEqualTo(self.contentView).offset(-10)
        }
 
    }
}


class ChatTextCell:ChatBasicCell {
    var contentLabel:UILabel = UILabel()
    var contentBackgroundView = UIView()
    override func setUpUI() {
        super.setUpUI()
        contentView.addSubview(contentBackgroundView)
        contentView.addSubview(contentLabel)
        contentLabel.numberOfLines = 0
        contentLabel.textColor = UIColor.whiteColor()
        contentBackgroundView.backgroundColor = Style.textColor3
        contentBackgroundView.layer.cornerRadius = 4
        contentBackgroundView.layer.masksToBounds = true
        contentLabel.snp_remakeConstraints { (make) in
            make.height.greaterThanOrEqualTo(31.5)
            make.leading.equalTo(self.headPotrait.snp_trailing).offset(10)
            make.top.equalTo(self.headPotrait).offset(7)
            make.trailing.equalTo(self.contentView).offset(-80)
            make.bottom.equalTo(self.contentView).offset(-20)
        }
        contentBackgroundView.snp_makeConstraints { (make) in
            make.center.equalTo(contentLabel)
            make.width.equalTo(contentLabel).offset(16)
            make.height.equalTo(contentLabel).offset(16)
        }
    }
    
    override func adjustContent() {
        super.adjustContent()
        contentLabel.snp_remakeConstraints { (make) in
            make.height.greaterThanOrEqualTo(31.5)
            if fromMe {
                contentBackgroundView.backgroundColor = Style.textColor3
                make.trailing.equalTo(self.headPotrait.snp_leading).offset(-18)
                make.leading.equalTo(self.contentView).offset(80)
            } else{
                contentBackgroundView.backgroundColor = Style.themeColor1
                make.leading.equalTo(self.headPotrait.snp_trailing).offset(18)
                make.trailing.equalTo(self.contentView).offset(-80)
            }
            make.top.equalTo(self.headPotrait).offset(7)
            
            make.bottom.equalTo(self.contentView).offset(-20)
        }
    }
}

class ChatImageCell:ChatBasicCell {
    var contentImageView = UIImageView()
    
    override func setUpUI() {
        super.setUpUI()
        contentView.insertSubview(contentImageView, atIndex: 0)
        contentImageView.layer.cornerRadius = 4
        contentImageView.layer.masksToBounds = true
        contentImageView.snp_remakeConstraints { (make) in
            make.leading.equalTo(self.headPotrait).offset(10)
            make.top.equalTo(self.headPotrait).offset(7)
            make.width.equalTo(260)
            make.height.equalTo(200)
            make.bottom.equalTo(self.contentView).offset(-20)
        }
    }
    
    override func adjustContent() {
        super.adjustContent()
        contentImageView.snp_remakeConstraints { (make) in
            if fromMe {
                make.trailing.equalTo(self.headPotrait).offset(-10)
            } else {
                make.leading.equalTo(self.headPotrait).offset(10)
            }
            make.top.equalTo(self.headPotrait).offset(7)
            make.width.equalTo(260)
            make.height.equalTo(200)
            make.bottom.equalTo(self.contentView).offset(-20)
        }
    }
}